package settings;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author A0098808L Yuen Shu Hui
 */
public class Settings {

	private static Settings instance = null;
	public static final DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
	public static final DecimalFormat decFmt = new DecimalFormat("#.##");
	public static final Calendar cal = Calendar.getInstance();
	public static final Calendar lastActiveDate = Calendar.getInstance();

	protected Settings(){

	}
	
	/**
	 * Settings is a singleton object
	 * @author A0098808L Yuen Shu Hui
	 * @return
	 */
	public static Settings getInstance(){
		if(instance == null){
			instance = new Settings();
		}
		return instance;
	}

	/**
	 * Utility to convert a double into 2 decimal place
	 * @author A0098808L Yuen Shu Hui
	 * @param _input
	 * @return
	 */
	public static String doubleTo2DP(double _input)
	{
		String formated = decFmt.format(_input);
		int ind = formated.indexOf(".");

		if(ind == -1)
			formated += ".00";
		else if(ind == formated.length() - 2)
			formated += "0";

		return formated;
	}

	/**
	 * Utility to convert A calendar object to Date in simple format
	 * @author A0098808L Yuen Shu Hui
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String dateFormatter(Calendar _date)
	{
		return df.format(_date.getTime());
	}

	/**
	 * Utility to calculate date difference in terms of day
	 * @author A0098808L Yuen Shu Hui
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int daysBetween(Calendar startDate, Calendar endDate) {  
		Calendar date = (Calendar) startDate.clone();  
		int daysBetween = 0;  
		while (date.before(endDate)) {  
			date.add(Calendar.DAY_OF_MONTH, 1);  
			daysBetween++;  
		}  
		return daysBetween;  
	} 

}
